<template>
  <div class="page-box">
    <el-card class="page-box-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        label-width="88px"
        inline
        @submit.native.prevent
      >
        <el-form-item label="项目编号" prop="param.projectNum">
          <el-input
            v-model="queryParams.param.projectNum"
            placeholder="请输入项目编号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="param.topicName">
          <el-input
            v-model="queryParams.param.topicName"
            placeholder="请输入项目名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目类别" prop="param.category">
          <el-select
            v-model="queryParams.param.category"
            placeholder="请选择项目类别"
          >
            <el-option
              v-for="item in dict.type.project_category"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目状态" prop="param.status">
          <el-select
            v-model="queryParams.param.status"
            placeholder="请选择项目状态"
          >
            <el-option
              v-for="item in dict.type.auditing_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['definition:class:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
            v-hasPermi="['definition:class:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['definition:class:delete']"
            >删除</el-button
          >
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload2"
            size="mini"
            v-hasPermi="['definition:class:upload']"
            >导入</el-button
          >
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="hanldeExport"
            v-hasPermi="['definition:class:download']"
            >导出</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="dataList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column type="index" label="序号" width="60" fixed="left" />
        <el-table-column
          prop="projectNum"
          label="项目编号"
          width="180"
          fixed="left"
        />
        <el-table-column prop="topicShortName" label="项目简称" width="180" />
        <el-table-column prop="type" label="类型" width="160">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.project_type"
              :value="scope.row.type"
            />
          </template>
        </el-table-column>
        <el-table-column prop="category" label="类别" width="160">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.project_category"
              :value="scope.row.category"
            />
          </template>
        </el-table-column>
        <el-table-column prop="leadingUnit" label="牵头单位" />
        <el-table-column prop="person" label="负责人" width="120" />
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.auditing_type"
              :value="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column prop="sptStatus" label="上报省平台状态" width="120">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.sptStatus == '1'">
              已上报
            </el-tag>
            <el-tag type="info" v-else-if="scope.row.sptStatus == '0'">
              未上报
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="nfwStatus" label="上报脑防委状态" width="120">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.nfwStatus == '1'">
              已上报
            </el-tag>
            <el-tag type="info" v-else-if="scope.row.nfwStatus == '0'">
              未上报
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="240">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.status == '2'"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >修改</el-button
            >
            <el-button
              :disabled="scope.row.status == '2'"
              size="mini"
              type="text"
              :style="{color: scope.row.status == '2' ? '#c0c4cc' : 'red'}"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:edit']"
              >删除</el-button
            >
            <el-button
              :disabled="scope.row.status != '0'"
              size="mini"
              type="text"
              icon="el-icon-thumb"
              @click="handleChange(scope.row, '项目详情', '1')"
              v-hasPermi="['system:dept:edit']"
              >提交</el-button
            >
            <el-button
              :disabled="scope.row.status != '1'"
              size="mini"
              type="text"
              icon="el-icon-s-check"
              @click="handleChange(scope.row, '项目审核', '2')"
              v-hasPermi="['system:dept:edit']"
              >审核</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改部门对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addDialog"
      width="1200px"
      append-to-body
    >
      <el-form ref="addForm" :model="addForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目编号" prop="projectNum">
              <el-input
                v-model="addForm.projectNum"
                placeholder="请输入项目编号"
                @blur="getProjectNum"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目名称" prop="topicName">
              <el-input
                v-model="addForm.topicName"
                placeholder="请输入项目名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目简称" prop="topicShortName">
              <el-input
                v-model="addForm.topicShortName"
                placeholder="请输入项目简称"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="立项年度" prop="projectYear">
              <el-date-picker
                v-model="addForm.projectYear"
                style="width: 100%"
                value-format="yyyy"
                type="year"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="addForm.type"
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in dict.type.project_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value - 0"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类别" prop="category">
              <el-select
                v-model="addForm.category"
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in dict.type.project_category"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="牵头单位" prop="leadingUnit">
              <el-input
                v-model="addForm.leadingUnit"
                placeholder="请输入牵头单位"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人" prop="person">
              <el-input
                v-model="addForm.person"
                placeholder="请输入负责人"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人手机号" prop="chargePhone">
              <el-input
                v-model="addForm.chargePhone"
                placeholder="请输入负责人手机号"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="负责人职务" prop="chargePost">
              <el-input
                v-model="addForm.chargePost"
                placeholder="请输入负责人职务"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人邮箱" prop="chargeEmail">
              <el-input
                v-model="addForm.chargeEmail"
                placeholder="请输入负责人邮箱"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人科室" prop="chargeDept">
              <el-input
                v-model="addForm.chargeDept"
                placeholder="请输入负责人科室"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目开始时间" prop="startTime">
              <el-date-picker
                placeholder="请选择"
                v-model="addForm.startTime"
                style="width: 100%"
                value-format="yyyy-MM-dd"
                type="date"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目结束时间" prop="endTime">
              <el-date-picker
                placeholder="请选择"
                v-model="addForm.endTime"
                style="width: 100%"
                value-format="yyyy-MM-dd"
                type="date"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目状态" prop="status">
              <el-select
                v-model="addForm.status"
                style="width: 100%"
                placeholder="请选择"
                disabled
              >
                <el-option
                  v-for="item in dict.type.auditing_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="24">
            <el-form-item label="下级单位添加">
              <el-input placeholder="请输入下级单位添加"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="子级任务添加">
              <el-input placeholder="请输入子级任务添加"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="添加附件" prop="status">
              <!-- <el-input
                v-model="addForm.status"
                placeholder="请输入项目状态"
              ></el-input> -->
              <FileUpload v-model="addForm.file" :limit="1" :fileSize="50" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="changeStatus == '0'" type="primary" @click="submitForm">确 定</el-button>
        <el-button v-else-if="changeStatus == '1'" type="primary" @click="submitChange('1')">提 交</el-button>
        <template v-else-if="changeStatus == '2'">
          <el-button type="primary" @click="submitChange('2')">通 过</el-button>
          <el-button type="danger" @click="submitChange('3')">驳 回</el-button>
        </template>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  insertProject,
  updateProject,
  deleteProject,
  listProject,
  queryByNum,
  exportData
} from "@/api/data/topic";
export default {
  dicts: ["auditing_type", "project_type", "project_category"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectionData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          name: "",
        },
      },
      props: {
        checkStrictly: true,
        label: "name",
        value: "id",
      },
      dataList: [],
      total: 0,
      treeData: [],
      title: "",
      addDialog: false,
      addForm: {
        name: "",
        topicName: '',
        topicShortName: '',
        projectYear: '',
        type: '',
        category: '',
        leadingUnit: '',
        person: '',
        chargePhone: '',
        chargePost: '',
        chargeEmail: '',
        chargeDept: '',
        startTime: null,
        endTime: null,
        status: null,
        file: '',
      },
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
      changeStatus: '0',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 导出
    hanldeExport() {
      const that = this;
      this.$modal.confirm("是否确认导出当前查询条件下的数据？").then(() => {
        // 导出
        const params = that.queryParams;
        // return that.download(exportData, { ...params }, `接口分类.xlsx`);
        exportData({ ...params })
          .then((res) => {
            // const url = window.URL.createObjectURL(new Blob([res]));
            const blob = new Blob([res]);
            this.$download.saveAs(blob, "项目管理.xlsx");
            this.$message.success("导出成功");
          })
          .catch((error) => {
            this.$message.success("下载Excel失败:", error);
            console.error("下载Excel失败:", error);
          });
      });
    },
    handleChange(row, title, status) {
      this.title = title;
      const data = row.id ? row : this.selectionData[0];
      this.addForm = {
        ...data,
        status: data.status + "",
      };
      this.changeStatus = status;
      this.addDialog = true;
    },
    // 新增项目编号查重事件
    getProjectNum(e) {
      if (this.addForm.id) {
        return;
      }
      const projectNum = e.target.value;
      queryByNum({ projectNum }).then((res) => {
        console.log("res: ", res);
      });
    },
    // 搜索事件
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置事件
    resetQuery() {
      this.dataList = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loading = true;
      const param = { ...this.queryParams };
      console.log("param: ", param);
      listProject(param).then((response) => {
        console.log("response: ", response);
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 列表选中事件
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selectionData = [...selection];
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 新增项目
    handleAdd() {
      this.title = "新增项目";
      this.addForm = {
        name: "",
        topicName: '',
        topicShortName: '',
        projectYear: '',
        type: '',
        category: '',
        leadingUnit: '',
        person: '',
        chargePhone: '',
        chargePost: '',
        chargeEmail: '',
        chargeDept: '',
        startTime: null,
        endTime: null,
        status: '0',
        file: '',
      }
      // this.addForm.
      // this.resetForm('addForm');
      this.changeStatus = '0';
      this.addDialog = true;
    },
    // 新增/修改弹框提交事件
    submitForm() {
      const param = { ...this.addForm };
      if (param.id) {
        // 修改
        updateProject(param).then((res) => {
          this.$message.success("修改成功");
          this.cancel();
          this.handleQuery();
        });
      } else {
        // 新增
        insertProject(param).then((res) => {
          this.$message.success("新增成功");
          this.cancel();
          this.handleQuery();
        });
      }
    },
    // 新增/修改弹框提交事件
    submitChange(status) {
      const param = { ...this.addForm };
      param.status = status
      // 修改
      updateProject(param).then((res) => {
        let str = status == '1' ? '提交' : '审核'
        this.$message.success(str + "成功");
        this.cancel();
        this.handleQuery();
      });
    },
    // 新增/修改弹框取消事件
    cancel() {
      this.addForm = {
        name: "",
        topicName: '',
        topicShortName: '',
        projectYear: '',
        type: '',
        category: '',
        leadingUnit: '',
        person: '',
        chargePhone: '',
        chargePost: '',
        chargeEmail: '',
        chargeDept: '',
        startTime: null,
        endTime: null,
        status: null,
        file: '',
      };
      this.addDialog = false;
    },
    // 修改项目
    handleEdit(row) {
      this.title = "修改项目";
      const data = row.id ? row : this.selectionData[0];
      this.addForm = {
        ...data,
        status: data.status + "",
      };
      this.changeStatus = '0';
      this.addDialog = true;
    },
    // 删除项目
    handleDelete(row) {
      const idList = row.id ? [row.id] : [...this.ids];
      this.$modal
        .confirm("是否确认删除当前选择的项目？")
        .then(function () {
          // 删除
          return deleteProject({ idList });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.handleQuery();
        })
        .catch(() => {});
    },
  },
};
</script>
